package com.shenhaofeng.app.utils.io;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;


/**
 * Created by Administrator on 2015/12/23.
 */
public class IOUtil {

    /**
     * 安全关闭数据流
     *
     * @param closeable
     */
    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 将文本写入输出流中
     *
     * @param outputStream
     * @param text
     * @return
     */
    public static boolean writeTextToOutputStream(OutputStream outputStream, String text) {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(outputStream);
        try {
            writer.write(text);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 将字节数据写入到输出流中
     *
     * @param outputStream
     * @param bytes
     * @return
     */
    public static boolean writeByteToOutputStream(OutputStream outputStream, byte[] bytes) {
        try {
            outputStream.write(bytes);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 将可序列化对象写入到输出流中
     * @param outputStream
     * @param serializable
     * @return
     */
    public static boolean writeSerializableToOutputStream(OutputStream outputStream, Serializable serializable) {
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(serializable);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 从流中读取序列化的对象
     *
     * @param inputStream
     * @param defaultSerializable
     * @return
     */
    public static Serializable readInputSteamToSerializable(InputStream inputStream, Serializable defaultSerializable) {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            Object result = objectInputStream.readObject();
            return Serializable.class.cast(result);
        } catch (IOException | ClassNotFoundException e) {
            return defaultSerializable;
        }
    }


    /**
     * 从数据流中读取文本
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String readInputSteamToText(InputStream inputStream, String defaultText) {
        try {
            byte[] bytes = readInputSteamToBytes(inputStream);
            return new String(bytes);
        } catch (IOException e) {
            return defaultText;
        }
    }


    /**
     * 从数据流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputSteamToBytes(InputStream inputStream) throws IOException {
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            buffer.write(bytes, 0, len);
        }
        return buffer.toByteArray();
    }


}
